﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WP8.MediaElement.Resources;
using System.Windows.Media;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.BackgroundAudio;
using System.IO.IsolatedStorage;
using System.Windows.Resources;

namespace WP8.MediaElement
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this.mediaElement.MediaFailed += mediaElement_MediaFailed;
            this.mediaElement.MediaOpened += mediaElement_MediaOpened;
        }

        private void mediaElement_MediaOpened(object sender, RoutedEventArgs e)
        {
            this.sldPosition.Maximum = this.mediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;
        }

        private void mediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
        {
            MessageBox.Show("Ocurrio un error al momento de abrir el archivo");
        }

        #region Methods
        private void Play(string uri)
        {
            this.mediaElement.Source = new Uri(uri, UriKind.RelativeOrAbsolute);
            var dime = this.mediaElement.Volume;
            this.mediaElement.Play();
        }
        #endregion 

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            string uri = null;
            if (rbLocal.IsChecked.Value)
            {
                uri = "/fwp.mp4";
            }
            else if (rbHTTP.IsChecked.Value)
            {
                uri = "http://video-js.zencoder.com/oceans-clip.mp4";
            }

            this.Play(uri);
        }
        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.Pause();
        }
        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.Stop();
        }

        private void btnMute_Click(object sender, RoutedEventArgs e)
        {
            this.mediaElement.IsMuted = this.btnMute.IsChecked.Value;
        }

        private void sldPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            int SliderValue = (int)e.NewValue;
            TimeSpan ts = new TimeSpan(0, 0, 0, 0, SliderValue);
            this.mediaElement.Position = ts;
        }

        private void btnLauncher_Click(object sender, RoutedEventArgs e)
        {
            MediaPlayerLauncher mediaPlayerLauncher = new MediaPlayerLauncher();
            mediaPlayerLauncher.Media = new Uri("http://video-js.zencoder.com/oceans-clip.mp4", UriKind.Absolute);
            mediaPlayerLauncher.Controls = MediaPlaybackControls.All;
            mediaPlayerLauncher.Show();
        }

        private void btnBackground_Click(object sender, RoutedEventArgs e)
        {

            if (!IsolatedStorageFile.GetUserStoreForApplication().FileExists("Its my life.mp3"))
            {
                StreamResourceInfo resource = Application.GetResourceStream(new Uri("itsmylife.mp3", UriKind.Relative));

                using (IsolatedStorageFileStream file = IsolatedStorageFile.GetUserStoreForApplication().CreateFile("Its my life.mp3"))
                {
                    int chunkSize = 4096;
                    byte[] bytes = new byte[chunkSize];
                    int byteCount;

                    while ((byteCount = resource.Stream.Read(bytes, 0, chunkSize)) > 0)
                    {
                        file.Write(bytes, 0, byteCount);
                    }
                }
            }

            BackgroundAudioPlayer.Instance.Track = new AudioTrack(new Uri("Its my life.mp3", UriKind.RelativeOrAbsolute), "Its my life 2", "Bon Jovi 2", "Bon Jovi Album", null);
            BackgroundAudioPlayer.Instance.Volume = 1.0;
            BackgroundAudioPlayer.Instance.Play();
        }
    }
}